// ViewTaskList.cpp : implementation file
//

#include "stdafx.h"
#include "TravianAutoTask.h"
#include "ViewTaskList.h"


// CViewTaskList

IMPLEMENT_DYNCREATE(CViewTaskList, CListView)

CViewTaskList::CViewTaskList() 
	: m_pts(NULL)
	, m_bInited(false)
{
	// set image lists
	//m_imglst.Create ( IDB_TASKEXECSTATE, 32, 1, CLR_DEFAULT );
	//m_imglst.Create (IDB_TASKEXECSTATE, 32, 1, RGB(212, 208, 200));	
	m_imglst.Create(32, 32,  ILC_COLOR24 | ILC_MASK, 0, 1);
	m_bmp.LoadBitmap(IDB_TASKEXECSTATE);
	m_imglst.Add(&m_bmp, RGB(212, 208, 200));
}

CViewTaskList::~CViewTaskList()
{
	ClearTasks();
	m_imglst.DeleteImageList();
}

BEGIN_MESSAGE_MAP(CViewTaskList, CListView)
	//ON_WM_ERASEBKGND()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()


// CViewTaskList diagnostics

#ifdef _DEBUG
void CViewTaskList::AssertValid() const
{
	CListView::AssertValid();
}

#ifndef _WIN32_WCE
void CViewTaskList::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif
#endif //_DEBUG


//
// CViewTaskList message handlers
//
BOOL CViewTaskList::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class

	if (m_tt.m_hWnd != NULL)
		m_tt.RelayEvent(pMsg);
	return CListView::PreTranslateMessage(pMsg);
}

void CViewTaskList::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	CListCtrl& ListCtrl = GetListCtrl();

	LVHITTESTINFO lvht;
	lvht.pt.x = point.x;
	lvht.pt.y = point.y;

	if (ListCtrl.SubItemHitTest(&lvht) > -1 && lvht.iSubItem ==2 )
	{
		CRect rect;
		ListCtrl.GetSubItemRect(lvht.iItem, 2, LVIR_BOUNDS, rect);
		// CToolTipCtrl only affects the given rect area paramter of AddTool() fuction, 
		// so it commonly used in toolbar, menu, and dlg item which position are fixed.
		// So here emulate above situation by assigning each target sub-item a id, 
		// and fill the rect area of target sub-item in every AddTool() call.
		m_tt.AddTool(this, m_vecTasksInfo.at(lvht.iItem), &rect, m_vecToolTipID.at(lvht.iItem) );
	}

	CListView::OnMouseMove(nFlags, point);
}

//void CViewTaskList::OnRButtonUp(UINT nFlags, CPoint point)
void CViewTaskList::OnLButtonUp(UINT nFlags, CPoint point)
{
	CListCtrl& ListCtrl = GetListCtrl();

	LVHITTESTINFO lvht;
	lvht.pt.x = point.x;
	lvht.pt.y = point.y;

	if (ListCtrl.SubItemHitTest(&lvht) > -1 )
	{
		CTravianTask* ptask = m_pts->at(lvht.iItem).second;
		switch( lvht.iSubItem )
		{
		case 0:
			break;
		case 2:
			break;
		}
	}

}

BOOL CViewTaskList::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Add your specialized code here and/or call the base class

	// default is report view and full row selection
	cs.style &= ~LVS_TYPEMASK; 
	cs.style |= LVS_REPORT | LVS_EDITLABELS; //LVS_OWNERDRAWFIXED;	

	return CListView::PreCreateWindow(cs);
}

//BOOL CViewTaskList::OnEraseBkgnd(CDC* pDC)
//{
//	// TODO: Add your message handler code here and/or call default
//
//	CRect   rect;     
//	CBrush   brush;     
//	brush.CreateSolidBrush(g_crBkgnd);     
//	pDC->GetClipBox(rect);     
//	pDC->FillRect(rect, &brush);  
//	return   true;  
//
//	//return CListView::OnEraseBkgnd(pDC);
//}


void CViewTaskList::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: Add your specialized code here and/or call the base class

	CListCtrl& ListCtrl = GetListCtrl();

	// why OnInitialUpdate called 2 times?
	if (m_bInited) return;
	
	//
	// Init tool tip control
	//
	EnableToolTips(TRUE); 
	m_tt.Create(this);
	m_tt.SetDelayTime(TTDT_INITIAL, 0);
	m_tt.SetMaxTipWidth(350);
	m_tt.Activate(TRUE);


	// Set full row select style and grid line style. Default style is multiple selection support
	// Don't work well when bare LVS_EX_BORDERSELECT (needs macro (_WIN32_IE >= 0x0500)) style and LVS_EX_CHECKBOXES style 
	ListCtrl.SetExtendedStyle(ListCtrl.GetExtendedStyle() | LVS_EX_INFOTIP | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	// Set this list control report style image list
	ListCtrl.SetImageList(&m_imglst, LVSIL_SMALL);

	ListCtrl.SetBkColor(g_crBkgnd);
	ListCtrl.SetTextBkColor(g_crBkgnd);

// insert columns
	LV_COLUMN lvc;
	lvc.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

	lvc.iSubItem = 0;
	lvc.pszText = _T("Exec State");
	lvc.cx = 90;
	ListCtrl.InsertColumn(0,&lvc);

	lvc.iSubItem = 1;
	lvc.pszText = _T("NO.");
	lvc.cx = 25;
	ListCtrl.InsertColumn(1,&lvc);

	lvc.iSubItem = 2;
	lvc.pszText = _T("Task Description");
	lvc.cx = 650;
	ListCtrl.InsertColumn(2,&lvc);

	m_bInited = true;

}

void CViewTaskList::SetTasks(Tasks* pts)
{
	ClearTasks();
	CListCtrl& ListCtrl = GetListCtrl();
	ListCtrl.DeleteAllItems ();

	m_pts = pts;
	if (NULL == m_pts) return;

	//sort(m_pts->begin(),m_pts->end());

	CString sTemp;
	for(UINT i=0; i< m_pts->size(); i++)
	{

	// insert items
		int nItem = ListCtrl.GetItemCount();
		LV_ITEM lvi;
		lvi.mask = LVIF_TEXT | LVIF_IMAGE ;
		lvi.iItem = nItem;
		lvi.iSubItem = 0;
		lvi.pszText = (LPTSTR)GetStateText( m_pts->at(i).second->m_eExecState );
		lvi.iImage = GetImgIndex( m_pts->at(i).second->m_eExecState );  // The img index in CImageList

		ListCtrl.InsertItem(&lvi);
		// set item text for additional columns
		sTemp.Format(_T("%d"), m_pts->at(i).first);
		ListCtrl.SetItemText(nItem, 1, sTemp);
		ListCtrl.SetItemText(nItem, 2, m_pts->at(i).second->GetTaskStr());

		m_vecTasksInfo.push_back( m_pts->at(i).second->GetTaskStr() );
		m_vecToolTipID.push_back( 2000+i);

	}

	//UpdateWindow();
}
void CViewTaskList::SetTaskState(int nIndex, CTravianTask *ptask)
{
	CListCtrl& ListCtrl = GetListCtrl();

	LV_ITEM lvi;
	lvi.mask = LVIF_TEXT | LVIF_IMAGE ;
	lvi.iItem = nIndex;
	lvi.iSubItem = 0;
	lvi.pszText = (LPTSTR)GetStateText( ptask->m_eExecState );
	lvi.iImage = GetImgIndex( ptask->m_eExecState );  // The img index in CImageList

	ListCtrl.SetItem(&lvi);
	ListCtrl.SetItemText(nIndex, 2, ptask->GetTaskStr());
	delete ptask;
}





